package com.jianglin.mapper;

import com.jianglin.model.Repair;
import com.jianglin.model.Tongji;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@SuppressWarnings("SpellCheckingInspection")
@Component("repairDao")
public interface RepairMapper {
//    @Select("<script>" +
//            "SELECT rep.*, own.username, type.name " +
//            "FROM repair rep " +
//            "LEFT JOIN repairtype type ON type.id = rep.com_id " +
//            "LEFT JOIN owner own ON own.id = rep.owner_id " +
//            "WHERE rep.owner_id = #{ownerId} " +
//            "<if test='comId != null'> AND rep.com_id = #{comId} </if>" +
//            "<if test='status != null'> AND rep.status = #{status} </if>" +
//            "ORDER BY rep.com_date DESC" +
//            "</script>")
//    @Select("<script>" +
//            "SELECT rep.*, own.username, type.name " +
//            "FROM repair rep " +
//            "LEFT JOIN repairtype type ON type.id = rep.com_id " +
//            "LEFT JOIN owner own ON own.id = rep.owner_id " +
//            "<where>" +
//            "   <!-- 管理员不限制业主ID -->" +
//            "   <if test='isAdmin == false'> AND rep.owner_id = #{ownerId} </if>" +
//            "   <if test='comId != null'> AND rep.com_id = #{comId} </if>" +
//            "   <if test='status != null'> AND rep.status = #{status} </if>" +
//            "</where>" +
//            "ORDER BY rep.com_date DESC" +
//            "</script>")
//    @Results({
//            @Result(property = "id", column = "id"),
//            @Result(property = "owner", column = "owner_id",
//                    one = @One(select = "com.jianglin.mapper.OwnerMapper.findOwnerById")),
//            @Result(property = "type", column = "com_id",
//                    one = @One(select = "com.jianglin.mapper.RepairtypeMapper.findTypeById"))
//    })
//    List<Repair> queryRepairAll(Repair repair);
    @Select("<script>" +
            "SELECT rep.*, own.username, type.name " +
            "FROM repair rep " +
            "LEFT JOIN repairtype type ON type.id = rep.com_id " +
            "LEFT JOIN owner own ON own.id = rep.owner_id " +
            "<where>" +
            "   <!-- 非管理员只能查看自己的报修 -->" +
            "   <if test='isAdmin == false'> AND rep.owner_id = #{ownerId} </if>" +
            "   <if test='comId != null'> AND rep.com_id = #{comId} </if>" +
            "   <if test='status != null'> AND rep.status = #{status} </if>" +
            "</where>" +
            "ORDER BY rep.com_date DESC" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "owner", column = "owner_id",
                    one = @One(select = "com.jianglin.mapper.OwnerMapper.findOwnerById")),
            @Result(property = "type", column = "com_id",
                    one = @One(select = "com.jianglin.mapper.RepairtypeMapper.findTypeById"))
    })
    List<Repair> queryRepairAll(Repair repair);

    @Select("SELECT count(info.id) AS counts, type.NAME " +
            "FROM repair info " +
            "LEFT JOIN repairtype type ON type.id = info.com_id " +
            "GROUP BY info.com_id")
    List<Tongji> queryTongji();

    @Insert("INSERT INTO repair (com_id, com_date, handle_date, owner_id, status, clr, remarks) " +
            "VALUES (#{comId}, #{comDate}, #{handleDate}, #{ownerId}, #{status}, #{clr}, #{remarks})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Repair repair);

    @Delete("DELETE FROM repair WHERE id = #{id}")
    int delete(Long id);

    @Update("UPDATE repair SET status=#{status}, handle_date=#{handleDate} WHERE id=#{id}")
    int updateData(Repair repair);

    @Select("SELECT * FROM repair WHERE id = #{id}")
    Repair findById(Long id);

}
